package com.cskaoyan.controller;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.domain.Goods;
import com.cskaoyan.bean.goods.BasePageData;
import com.cskaoyan.bean.goods.CatAndBrandVo;
import com.cskaoyan.bean.goods.GoodsAddBO;
import com.cskaoyan.bean.goods.GoodsEchoVo;
import com.cskaoyan.service.GoodsService;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @BelongProject: mall35th
 * @BelongPackage: com.cskaoyan.controller
 * @Author: zijin
 * @Createtime: 2021-11-29 星期一 20:46
 * @Description: 商品模块
 */
@RequestMapping("admin/goods")
@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     *http://182.92.235.201:8083/admin/goods/list?page=1&limit=20&goodsSn=&name=%E9%92%9F&sort=add_time&order=desc
     * 查询条件关键字：
     * goodsId 商品id
     * name 商品名称
     * goodsSn 商品编号
     *
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo<BaseData> list(Integer goodsId, String goodsSn, String name, BaseParam pageParams){
        /**
         * 优惠券界面会调用到此类
         */
        BasePageData goodsList = goodsService.queryGoodsByCondition(goodsId,goodsSn,name,pageParams);
        return BaseRespVo.ok(goodsList);
    }

    /**
     * 商品信息回显
     * 根据商品id查询商品参数表
     * 根据商品id查询商品信息
     * http://182.92.235.201:8083/admin/goods/detail?id=1110016
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        GoodsEchoVo goodsEchoVo = goodsService.queryDetails(id);
        return BaseRespVo.ok(goodsEchoVo);
    }


    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand(){
        CatAndBrandVo catAndBrandVo = goodsService.queryCatgoryAndBrand();
        return BaseRespVo.ok(catAndBrandVo);
    }

    /**
     * 商品上架
     * @return
     */
    @SneakyThrows
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GoodsAddBO goodsAddBO){
        boolean flag = goodsService.createGoods(goodsAddBO);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GoodsAddBO goodsAddBO){
        boolean flag = goodsService.updateGoods(goodsAddBO);
        if (flag){
            return BaseRespVo.ok(null);
        }else {
            return BaseRespVo.error();
        }
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Goods goods){
        boolean flag = goodsService.deleteGoods(goods);
        if (flag){
            return BaseRespVo.ok(null);
        }else {
            return BaseRespVo.error();
        }
    }
}
